package com.hjm.admin.moule.view.fragment.impl;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hjm.admin.moule.R;
import com.hjm.admin.moule.db.helpter.CollBookHelper;
import com.hjm.admin.moule.model.LocalFileBean;
import com.hjm.admin.moule.utils.FileUtils;
import com.hjm.admin.moule.utils.LoadingHelper;
import com.hjm.admin.moule.utils.rxhelper.RxUtils;
import com.hjm.admin.moule.view.adapter.LocalFileAdapter;
import com.hjm.admin.moule.view.base.BaseFileFragment;
import com.hjm.admin.moule.viewmodel.BaseViewModel;
import com.hjm.admin.moule.widget.theme.ColorButton;
import com.weavey.loading.lib.LoadingLayout;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class LocalBookFragment extends BaseFileFragment {
    @BindView(R.id.btn_scan)
    ColorButton mBtnScan;
    @BindView(R.id.rv_files)
    RecyclerView mRvFiles;
    @BindView(R.id.loadlayout)
    LoadingLayout loadLayout;
    List<LocalFileBean> mFilseBeans = new ArrayList<>();

    public static LocalBookFragment newInstance() {
        return new LocalBookFragment();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return setContentView(container, R.layout.fragment_local_book, new BaseViewModel(mContext));
    }

    @Override
    protected void initView() {
        mAdapter = new LocalFileAdapter(mFilseBeans);
        mRvFiles.setLayoutManager(new LinearLayoutManager(mContext));
        mRvFiles.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL));
        mRvFiles.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            String id = mFilseBeans.get(position).getFile().getAbsolutePath();
            if (CollBookHelper.getsInstance().findBookById(id) != null) return;
            mAdapter.setCheckedItem(position);
            if (mListener != null)
                mListener.onItemCheckedChange(mAdapter.getItemIsChecked(position));
        });
        mBtnScan.setOnClickListener(v -> {
            scanFiles();
        });
    }

    private void scanFiles() {
        LoadingHelper.getInstance().showLoading(mContext);
        addDisposable(FileUtils.getSDTxtFile()
                .compose(RxUtils::toSimpleSingle)
                .subscribe(files -> {
                    LoadingHelper.getInstance().hideLoading();
                    mFilseBeans.clear();
                    if (files.size() == 0) {
                        loadLayout.setStatus(LoadingLayout.Empty);
                    } else {
                        loadLayout.setStatus(LoadingLayout.Success);
                        for (File file : files) {
                            LocalFileBean localFileBean = new LocalFileBean();
                            localFileBean.setSelect(false);
                            localFileBean.setFile(file);
                            mFilseBeans.add(localFileBean);
                        }
                        mAdapter.notifyDataSetChanged();
                    }
                }));

    }
}
